//
//  CABtSeed.m
//  Test
//
//  Created by xgc on 12/25/17.
//  Copyright © 2017 xgc. All rights reserved.
//

#import "CABtSeed.h"

@implementation CABtSeed

+(id)initWithBtSeed: (BtSeed*)seed
{
    CABtSeed* btSeed = [[CABtSeed alloc]init];
    
    /*
    vector<uint8_t> vSeed = seed->getSeed();
    btSeed.seed = [[NSMutableData alloc]initWithBytes:&vSeed[0] length:vSeed.size()];
    */
    btSeed.seed = nil;
    
    /*
    NSMutableArray* mneArray = [NSMutableArray array];
    vector<string> vMne = seed->getMnemonicCode();
    int size = vMne.size();
    for(int i = 0; i < size; i++) {
        NSString* str = [NSString stringWithCString:vMne[i].c_str() encoding:[NSString defaultCStringEncoding]];
        [mneArray addObject:str];
    }
    
    btSeed.mnemonicCode = mneArray;
    */
    btSeed.mnemonicCode = nil;
    
    btSeed.creationTimeSeconds = seed->getCreationTimeSeconds();
    
    string pwdHash = seed->getPwdhash();
    btSeed.pwdhash = [NSString stringWithCString:pwdHash.c_str() encoding:[NSString defaultCStringEncoding]];
    
    vector<uint8_t> vSalt = seed->getRandomSalt();
    btSeed.randomSalt = [[NSMutableData alloc]initWithBytes:&vSalt[0] length:vSalt.size()];
    
    EncryptedData encryMne = seed->getEncryptedMnemonicCode();
    btSeed.encryptedMnemonicCode = [CAEncryptedData initWithEncryptedData:&encryMne];
    EncryptedData encrySeed = seed->getEncryptedSeed();
    btSeed.encryptedSeed = [CAEncryptedData initWithEncryptedData:&encrySeed];
    
    return btSeed;
}

-(BtSeed*)toBtSeed {
    BtSeed* btSeed = new BtSeed();
    
    void *buffer = NULL;
    size_t len = -1;
    if(self.seed != nil) {
        buffer = (void*)[self.seed bytes];
        len = [self.seed length];
    
        vector<uint8_t> seed((uint8_t*)buffer, (uint8_t*)buffer + len);
        btSeed->setSeed(seed);       
    }

    if(self.mnemonicCode != nil) {
        vector<string> vMne;
        for(id obj in self.mnemonicCode) {
            vMne.push_back([obj UTF8String]);
        }
        btSeed->setMnemonicCode(vMne);
    }

    btSeed->setCreationTimeSeconds(self.creationTimeSeconds);
    string pwdHash = [self.pwdhash UTF8String];
    btSeed->setPwdhash(pwdHash);
    
    buffer = (void*)[self.randomSalt bytes];
    len = [self.randomSalt length];
    vector<uint8_t> vSalt((uint8_t*)buffer, (uint8_t*)buffer + len);
    btSeed->setRandomSalt(vSalt);

    EncryptedData* encryMne = [self.encryptedMnemonicCode toEncryptedData];
    btSeed->setEncryptedMnemonicCode(*encryMne);
    delete encryMne;
    
    EncryptedData* encryptedSeed = [self.encryptedSeed toEncryptedData];
    btSeed->setEncryptedSeed(*encryptedSeed);
    delete encryptedSeed;
    
    return btSeed;
}

-(id)init {
    if (self = [super init]) {
        
    }
    return self;
}

@end
